import {
  storeBindingsBehavior
} from "mobx-miniprogram-bindings";
import base from "@src/store/base";
import {
  getBalanceRecordCount,
  getBalanceRecordList
} from "@src/api/member";
import router from "@src/router";
import dayjs from "dayjs";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor']
  }],
  data: {
    hasNextPage: false,
    loading: false,
    navWChoose: '',
    startDate: '2018-12',
    endDate: '2028-05',
    pickerStart: '2018-01',
    listData: [],
    pageNum: 1,
    pageSize: 10
  },
  methods: {
    onLogin() {
      this.getCurrentData();
      this.fetchData();
      this.balanceCount();
    },
    onLoad() {
      this.$scrollTop = this.selectComponent("#scroll-top");
    },
    onPageScroll(res) {
      this.$scrollTop.onPageScroll(res);
    },
    /* 上下拉刷新 */
    onPullDownRefresh() {
      wx.stopPullDownRefresh();
      this.setData({
        pageNum: 1,
        listData: []
      });
      this.fetchData();
      this.balanceCount();
    },
    onReachBottom() {
      if (this.data.hasNextPage) { // 为true则刷新数据
        this.setData({
          pageNum: this.data.pageNum + 1
        })
        this.fetchData();
      }
    },
    // 切换状态请求数据
    changeStatusGetData(e) {
      let index = e.currentTarget.dataset.index
      this.setData({
        navWChoose: index,
        pageNum: 1,
        listData: []
      })
      this.fetchData();
      this.balanceCount();
    },
    fetchData() { // listData接口
      let data = {
        time: this.data.startDate,
        type: this.data.navWChoose,
        page: this.data.pageNum,
        pageSize: this.data.pageSize
      }
      this.setData({
        loading: true
      })
      getBalanceRecordList(data).then(res => {
        if (res.code === 0) {
          let list = res.data.list
          /* 暂停刷新 */
          wx.stopPullDownRefresh()
          let listData = this.data.listData.concat(list)
          console.log('余额明细--1', listData)

          /* 排序从下往上：充值 => 赠送 => 升级赠送   逻辑未完善*/ 
          // let delItem = ''
          // for (let i = 0; i < listData.length; i++) {
          //   if (listData[i].recordType == '充值' && listData[i].createTime == listData[i + 1].createTime) {
          //     delItem = listData[i]
          //     listData.splice(i,1)
          //   }
          //   // listData.splice(i,0,delItem)
          // }
          // listData.splice(1,0,delItem)
          // console.log('delItem--', delItem)
          
          this.setData({
            loading: false,
            listData: listData,
            hasNextPage: res.data.hasNextPage
          })
          console.log('余额明细--3', listData)
        } else {
          this.setData({
            loading: false
          })
        }
      }).catch(() => {
        this.setData({
          loading: false
        })
      })
    },
    balanceCount() { //查询余额月度统计数据
      let data = {
        time: this.data.startDate,
      }
      getBalanceRecordCount(data).then(res => {
        if (res.code === 0) {
          this.setData({
            balanceCountData: res.data
          })
        }
      })
    },
    handleDateChange(event) {
      let startDate = event.detail.value
      if (startDate) {
        let chooseDateArr = startDate.split('-')
        this.setData({
          startDate,
          chooseDateArr,
          pageNum: 1,
          listData: []
        })
      } else {
        this.setData({
          startDate,
          pageNum: 1,
          listData: []
        })
      }
      this.fetchData();
    },
    //余额明细item点击
    showItemDetail(e) {
      let item = e.currentTarget.dataset.item;
      router.push({
        name: "member/balanceListDetail",
        success: (res) => {
          res.eventChannel.emit('item', item)
        }
      })
    },
    //获取当前时间
    getCurrentData() {
      let startDate = dayjs().format("YYYY-MM")
      let chooseDateArr = [dayjs().format("YYYY"), dayjs().format("MM")]
      this.setData({
        startDate,
        chooseDateArr
      })
    },
  }
})